/* Oggetto appendino */
function appendino (req) {
	var DOMOutputFormatter = req.responseXML;
	var html = document.getElementsByTagName ("html")[0];

	// Appendino del documento
	if (DOMOutputFormatter.getElementsByTagName("html").length != 0) {

		// Rimuove tutto cio' che e' dentro i tag <head> tranne gli
		// script.
		var head = html.getElementsByTagName("head")[0];
		var i = 0;
		while (i < head.childNodes.length)
			if (head.childNodes[i].nodeName != "script")
				head.removeChild (head.childNodes[i]);
			else
				i++;

		// Imposta head/title
		document.title = DOMOutputFormatter.title;

		// Farcisce il nostro <head> con la roba del nuovo <head>
		var newHead = DOMOutputFormatter.getElementsByTagName("head")[0];
		for (i = 0; i < newHead.childNodes.length; i++)
			head.appendChild (document.importNode (newHead.childNodes[i], true));

		// Gestione lista: ricorda layout / skin selezionata.
		var opzioni = DOMOutputFormatter.getElementById("opzioni");
		var pattern = ".//*[local-name() = 'option' and normalize-space(.) = '"+df[curDF].getCurrentSkin()+"' and parent::*[local-name() = 'optgroup' and normalize-space(@label) = '"+df[curDF].getCurrentLayout()+"']]";
		var skin = DOMOutputFormatter.evaluate (pattern, opzioni, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		if (skin.snapshotLength == 0) {
			var pattern = ".//*[local-name() = 'option' and normalize-space(.) = '"+df[curDF].getCurrentSkin()+"' and preceding-sibling::*[local-name() = 'option' and normalize-space(.) = '"+df[curDF].getCurrentLayout()+"']]";
			skin = DOMOutputFormatter.evaluate (pattern, opzioni, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
		}
		assert (skin.snapshotLength <= 1, "appendino selected: xpath fa di testa sua!");
		if (skin.snapshotLength == 1)
			skin.snapshotItem(0).setAttribute ("selected", "selected");
		else {
			//pattern = ".//*[local-name() = 'li' and normalize-space(.) = '"+df[curDF].getCurrentSkin()+"' and ancestor::*[local-name() = 'li' and normalize-space(.) = '"+df[curDF].getCurrentLayout()+"']]";
			pattern = ".//*[local-name() = 'li' and normalize-space(.) = '"+df[curDF].getCurrentSkin()+"']";
			skin = DOMOutputFormatter.evaluate (pattern, opzioni, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
			assert (skin.snapshotLength <= 1, "appendino selected: xpath fa di testa sua!");
			if (skin.snapshotLength == 1) {
				var pirulo = DOMOutputFormatter.createElementNS ("http://www.w3.org/1999/xhtml", "img");
				pirulo.setAttribute("src", "http://ltw0807.web.cs.unibo.it/df/xhtml/img/mini-arrow-right.gif");
				pirulo.setAttribute("alt", ">> ");
				skin.snapshotItem(0).insertBefore (pirulo, skin.snapshotItem(0).firstChild);
			}
		}

		newBody = document.importNode (DOMOutputFormatter.getElementsByTagName("body")[0], true);
		oldBody = document.getElementsByTagName("body")[0];

		html.replaceChild (newBody, oldBody);
	}

	// Appendino del frammento
	else {
		var wrapper = DOMOutputFormatter.documentElement;
		var cornice = document.getElementById("cornice");

		// Svuota div#cornice
		while (cornice.firstChild)
			cornice.removeChild(cornice.firstChild);

		// Riempie div#cornice
		while (wrapper.firstChild) {
			var newChild = wrapper.firstChild;
			cornice.appendChild(document.importNode(newChild, true));
			wrapper.removeChild(newChild);
		}
		
	}

	
	
}
